package great.easychat.help.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import great.easychat.help.R;
import great.easychat.help.WebUrlActivity;
import great.easychat.help.util.DebugLog;


/**
 * 隐私政策和用户协议弹窗
 */
public class ProtocolDialog {

    private AlertDialog dialog = null;

    public interface Callback {
        void onClickAgree();
        void onClickReject();
    }

    private Callback mcallback;


    /**
     * @param context  上下文
     * @param callback 回调
     */
    Activity activity;
    public void show(Activity context, final Callback callback) {
        if (dialog == null) {
            activity = context;
            dialog = new AlertDialog.Builder(context).create();
            mcallback = callback;
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog不消失
            dialog.setCancelable(true);//屏蔽返回按钮
            if (!context.isFinishing()) {
                dialog.show();
            }
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            window.setContentView(R.layout.dialog_protocol_login);
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            Button cancel = (Button)window.findViewById(R.id.btnCancel);
            Button sure  = (Button)window.findViewById(R.id.btnConfirm);
            TextView tvContent  = (TextView)window.findViewById(R.id.tvContent);
            sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    if (mcallback != null) {
                        mcallback.onClickAgree();
                    }
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    if (mcallback != null) {
                        mcallback.onClickReject();
                    }
                }
            });
            SpannableString spanText = new SpannableString(context.getResources().getString(R.string.protocol));
            spanText.setSpan(new ClickableSpan() {
                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(Color.RED);
                    ds.setUnderlineText(true);
                }
                @Override
                public void onClick(@NonNull View widget) {
                    DebugLog.d("onClick 111");
                    WebUrlActivity.start(context, "http://www.chattinghelper.cn/index.php?m=home&c=View&a=index&aid=70&admin_id=1&t=1610867700", "用户注册协议");
                }

            }, 34, 40, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            spanText.setSpan(new ClickableSpan() {
                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(Color.RED);
                    ds.setUnderlineText(true);
                }
                @Override
                public void onClick(@NonNull View widget) {
                    DebugLog.d("onClick 222");
                    WebUrlActivity.start(context, "http://www.chattinghelper.cn/index.php?m=home&c=View&a=index&aid=69&admin_id=1&t=1610867700", "隐私协议");
                }

            }, 41, 47, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            tvContent.setMovementMethod(LinkMovementMethod.getInstance());
            tvContent.setText(spanText);
        }
    }


    /**
     * 取消窗口
     */
    public void cancelDialog() {
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

}
